<?php
/*
 * @Description:
 * @Author: 橘子味的心
 * @Date: 2020-11-12 00:45:56
 * @LastEditTime: 2021-04-01 19:28:07
 * @LastEditors: 橘子味的心
 * @Reference:
 */

declare (strict_types = 1);

namespace app\system\controller;

use app\common\model\Message;
use think\facade\View;

/**
 * @title 后台首页
 */
class Index extends Base
{
    /**
     * @title   左菜单栏
     * @desc    左菜单栏
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function index()
    {
        if (request()->isAjax()) {
            /**
             * 左菜单栏
             */
            $auth = new Auth();
            $data = $auth->getRule();
            $app = request()->root();
            foreach ($data as &$v) {
                $v['url'] = empty($v['url']) ? '' : str_replace('\\', '/', str_replace('//', '/', $app . '/' . strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $v['url']))));
            }
            return tocode(200, 'ok', getTrees($data));
        }
        $info = [
            'bgcolorId' => cache('bgcolorId'),
        ];
        View::engine()->layout(false);
        return view('', $info);
    }
    /**
     * @title   主页一
     * @desc    控制台
     * @author  橘子味的心
     * @version 1.0
     * @method  GET
     */
    public function welcome()
    {
        $map = [['pid', '<>', 0]];
        $auth = new Auth();
        $data = $auth->getRule($map); //查询所有非按钮的栏目
        // $to_do_list = [
        //     ['title' => '待审评论', 'num' => '0'],
        //     ['title' => '待审帖子', 'num' => '0'],
        //     ['title' => '待审留言', 'num' => Message::where('status', 0)->count(), 'url' => 'Message/lst'],
        //     ['title' => '待审友情链接', 'num' => Links::where('status', 0)->count(), 'url' => 'Links/lst'],
        // ];
        $config = [
            [
                'title' => '网站名称',
                'content' => config_group('web_site_title'),
            ], [
                'title' => '系统版本',
                'content' => "v1.0.0",
            ], [
                'title' => 'ThinkPHP版本',
                'content' => app()->version(),
            ], [
                'title' => 'PHP版本',
                'content' => PHP_VERSION,
            ],
            [
                'title' => 'PHP运行方式',
                'content' => php_sapi_name(),
            ],
        ];
        $system_config = [
            [
                'title' => '服务器版本',
                'content' => php_uname('s') . php_uname('r'),
            ], [
                'title' => 'PHP安装路径',
                'content' => DEFAULT_INCLUDE_PATH,
            ], [
                'title' => 'Zend版本',
                'content' => Zend_Version(),
            ], [
                'title' => '服务器当前时间',
                'content' => date('Y-m-d H:i:s'),
            ], [
                'title' => '超时时间',
                'content' => get_cfg_var('max_execution_time') . '秒',
            ], [
                'title' => '内存限制',
                'content' => get_cfg_var('memory_limit'),
            ], [
                'title' => '服务器解释引擎',
                'content' => request()->server('SERVER_SOFTWARE'),
            ],
        ];
        $info = [
            'menu' => $data,
            // 'to_do_list' => $to_do_list,
            // 'message' => 0,
            'config' => $config,
            'system_config' => $system_config,
        ];
        View::engine()->layout(false);
        return view('', $info);
    }
    /**
     * @title   清除缓存
     * @desc    清除缓存
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function clear()
    {
        try {
            delete_dir_file(root_path('runtime'), true, ['session', 'log']);
        } catch (\Exception $e) {
            return tocode(100, '清除缓存失败');
        }
        return tocode(200, '清除缓存成功');
    }
}
